It may not have anything to do with the water heater. Try this test. Run the water in your sink and adjust it to a warm setting (mixing hot and cold water). See if the temperature fluctuates from hot to cold. If it does then there is a problem with the water heater. If it does not, the valve for the shower is in need of repair or replacement. The seals become worn in the mixing valve and expand and contract making the water got back and forth from hot to cold and vice versa, Delta one piece faucets are famous for this.
Maybe your pressure I n the neighborhood where you live is low for some reason and the nearest neighbor can cause this when turning on their water.
The hot and cold lines are probably switched.