It is done, if not often, because:
It may be against code in some areas; preventing water problems in an attic is a fearful theme.
A 40 gallon water heater weighs over 350 pounds; so it must be on a sturdy foundation.
It definitely requires a safety pan and drain to carry away any water from a relief velve or leak.
The electric wiring is best done with a disconnect nearby the unit.
Attics can get cold enough to freeze water pipes.