The joke is a pun. 'Moonlighting' in terms of a job means to have a second job in addition to one's regular employment.  Astronomers study space and all the objects within it.  Moonlight--the sun's light reflected off the moon--may be a thing an astronomer collects data on.
His day job was astronomy but at night time he was moonlighting .