Hydrogen sensor based on optical and electrical switching