Here's why:
* Magnesium (Mg) is in Group 2 of the periodic table, making it an alkaline earth metal. It has a +2 charge when it forms ions.
* Hydrogen (H) is in Group 1, and it forms a -1 charge (hydride ion, H⁻) when it forms ionic compounds.
To balance the charges, you need two hydride ions (2 x -1 = -2) to neutralize the +2 charge of the magnesium ion.