Apple offered this with iMac under the name Target Display Mode until around 2014, so that the iMac could simply be used as a display. However, in the course of further sealing off the macOS system, this option was removed.
With the latest update to version 4.5, the Luna app now offers the possibility to simply use a Mac as a display on another Mac – even via Ethernet or Thunderbolt cable, which should ensure a very stable connection. Important to note: the small hardware dongle that Luna offers is still necessary.
So you can now use a MacBook as a display on a MacBook, a MacBook on an iMac, or a MacBook as a display for a Mac mini. However, one restriction has to be mentioned: if you use an iMac as a display, it can only be operated with normal resolution. Retina resolution at 4K or 5K resolutions is not supported. The hardware performance of the small Luna dongle is probably not sufficient for this.