Chemistry - How does hydrogen gas build up in hot water lines?
Solution 1:
I think that it is probably due to a reaction between an acid and a metal. These reactions can lead to the formation of hydrogen following this generic forumula: $$\ce{M + 2H+ -> M^{2+} + H2}$$
For example iron could be a source of electrons. If you look at the corrosion of iron: $$\ce{Fe->Fe^{2+} + 2e^{-}}$$
At low pH (notice that there is a correlation between pH and temperature) hydrogen gas is formed: $$\ce{2H^{+} +2e- -> 2H2}$$
Solution 2:
Water reacts with the magnesium sacrificial anode over time to make $\ce{H2}$. Normally, you'd be using your hot water often enough that you wouldn't notice the minute amounts of $\ce{H2}$ coming out the tap with the hot water. But if your place is vacant for some time, a significant amount of $\ce{H2}$ could build up and an ignitable mixture come out the first time a tap is turned on (or a dishwasher, which is a case study of an explosion occurring such that the appliance rocketed out from its mooring and hit the opposite kitchen wall).
Depending on the chemistry of your water, the reaction with the anode might be vigorous such that you could be getting puffs of hydrogen out of every use of the hot tap enough to set off "flammable gas" alarms. In this case or otherwise excessive consumption of your anode, switching to an aluminum anode may help.
In addition, bacteria can use the $\ce{H2}$ as food and fart out $\ce{H2S}$ instead, giving the water a "rotten egg" odor instead. The aluminum anode switch can help with this too, or temporarily setting the heater to 160F for a day to kill the bugs (although exotic bacteria of this type can survive at high temp, and furthermore the $\ce{H2S}$ may be from the supply instead if the tank if the cold stinks too).
And yes, this has nothing to do with gas vs. electric heaters, it's purely the chemical reaction between water and the anode.
Summary: Run your hot tap for a few minutes after leaving your house vacant for some time; switch to an aluminum sacrificial anode.
Solution 3:
You'd think that the main source of hydrogen is the sacrificial anode in your water heater.