Yes — vitamin D and magnesium are commonly taken together and are not expected to interact dangerously (magnesium actually helps your body activate vitamin D), but stay within sensible doses and check with your pharmacist or doctor.
These two nutrients are complementary rather than competing: magnesium is an essential cofactor for the cytochrome P450 (CYP) enzymes that synthesize and metabolize vitamin D — a randomized trial in NIH/PMC reports that vitamin D-metabolizing enzymes such as 1-alpha-hydroxylase and 24-hydroxylase are magnesium-dependent — so key steps of vitamin D metabolism rely on adequate magnesium. The NHS and MedlinePlus describe each nutrient on its own without flagging a dangerous interaction when they are taken together, and neither blocks the other's absorption (MedlinePlus actually notes that a diet high in vitamin D increases the body's need for magnesium). The real risks come from taking too much of either nutrient, not from combining them — so this is about staying within safe dose limits rather than avoiding the pair.
Mind the upper limits. The NHS advises most adults need about 10 mcg (400 IU) of vitamin D a day and to not exceed 100 mcg (4,000 IU)/day, because chronic excess causes calcium build-up (hypercalcaemia) that can weaken bones and damage the kidneys and heart. For magnesium, the NHS says 400 mg or less a day from supplements is unlikely to harm, while higher doses commonly cause diarrhoea (and can bring cramping or nausea). People with reduced kidney function are the key exception: MedlinePlus notes that side effects from high magnesium intake are uncommon except in people with significantly reduced kidney function, in whom excess magnesium can build up to harmful levels (hypermagnesemia) because the kidneys cannot clear it — so anyone with chronic kidney disease, or who is pregnant, taking medications, or already on a multivitamin/combined product, should talk to a clinician or pharmacist before adding these. Watch for signs of overdoing it (persistent diarrhoea, nausea, confusion, muscle weakness, irregular heartbeat) and seek medical advice if they appear. Supplements are not a substitute for medical care for a diagnosed deficiency.
This is general reference, not medical advice, and not a guarantee of safety. Interactions depend on your doses, health conditions, and other medicines. Always confirm with your pharmacist or doctor before combining products, and follow the dosing on each label.