The oxidation number is the charge left on the atom of interest when we conceptually break the "element-element" bonds with the charge (the electrons!) assigned to the most electronegative atom.
Do this for water H-O-H, and we get H^+ + HO^-; do this again for hydroxide, we get H^+ + O^(2-). And thus the oxidation numbers for hydrogen and oxygen in water are +I and -II. I reiterate that this is an entirely conceptual exercise. The rigmarole can sometimes help to balance redox equations.
In metal hydrides, MH, MH_2, the oxidation of hydrogen is -I (because here it is the more electronegative element and gets the electron).
In peroxides, i.e. HO-OH, because the electrons are conceived to be shared in the peroxo, O-O, linkage, the oxidation number of O is -I.
Of course for "elemental hydrogen", H_2, and for "elemental oxygen", O_2, neither element has accepted nor donated electrons. The oxidation state of an element is a big fat 0.
When these 2 elements react together, when the oxygen is "reduced" and the hydrogen is "oxidized", electron transfer is conceived to have occurred, and the two elements are now assigned their normal oxidation numbers, which are?
H_2(g) + 1/2O_2(g) rarr H_2O(l)