Electronic stability of metal-dihydrogen and -polyhydrogen complexes