Yes, you can take calcium and magnesium together, and for most people it is perfectly fine. They are often sold as a combined tablet, sometimes with vitamin D added. The main things worth knowing are about dose and about what else you take alongside them, rather than about the pair itself.
The "2 to 1 ratio" is marketing, not a rule
You will see calcium and magnesium sold in a fixed two-to-one ratio, presented as if the body requires it. It does not. The ratio is a convention, not a physiological requirement, and there is no good evidence you need to hit a specific calcium-to-magnesium number. What matters is whether you are short of either one in the first place, which comes down to your diet rather than the label.
Where dose does matter
At very high doses, calcium and magnesium do compete a little for absorption, so taking large amounts of both at the exact same moment is slightly less efficient than spacing them out. For the ordinary doses in most supplements, this is a minor effect and not worth worrying about. If you take a large calcium dose, splitting it through the day absorbs better anyway, since the body can only take up so much calcium at once.
What to keep them away from
The bigger timing question is not calcium versus magnesium, but calcium versus a few other things. Calcium reduces the absorption of iron and of thyroid medication when taken together, so if you take either of those, keep your calcium a few hours apart from them. Magnesium, especially the gentler glycinate form, pairs naturally with vitamin D.
To check calcium or magnesium against any medication you take, the free checker will show you the result and the reasoning.