RAW using eye rays does not count as attacks or spells, since they aren’t spells and require saves, not to-hit rolls.

This has been confirmed in official WotC products where Beholders have access to invisibility.

In those products, the authors acknowledge that the tactic is cheesy as hell, but the beholder in question is not actually part of the adventure objectives, the PCs are told it is incredibly dangerous, and if they want to fight it, play it as evil as possible.