How to colour entire object based on its height
Achieving this with material nodes
I thought Generated and Object texture spaces differ and that can be used for this. Thanks to Robin Betts for figuring out the correct math.
Object's texture space goes from 0 to one in every direction on every axis from the origin of the object to the end of its bounding box in that axis, however Generated texture space matches the scale of global world space. If the origins of all objects are at their bottoms, their heights can be calculated by dividing the Z value of the object's texture space by the Z value of Generated texture space. The values can then be compressed to the desired range by dividing them by the height of the highest object. A Color Ramp node then can be used to remap the height values to colors: