Wire size for water heater
8 AWG copper wire or 6 AWG aluminum. For the ground wire, 10 AWG copper.

Most on-demand heaters need 2 wires + ground, but check your installation instructions to be sure it doesn't need 3. 

Keep in mind a 40A on-demand is a pretty small heater and you may need to reduce flow to get hot enough water.