Solgaleo and Lunala have different roles in competitive, so either is not directly better than the other, individually.
However, in all the tiers they are allowed, Necrozma-Dusk Mane is also present which is virtually better than Solgaleo in almost every regard—possessing a much superior ability, having access to Stealth Rock, as well as having access to Ultra Burst and Swords Dance, and later Dragon Dance in Gen 8+, which makes it a better offensive presence than Solgaleo. As such, you would normally not have a sufficient reason to consider using Solgaleo over Necrozma-DM. Lunala on the hand, can fulfill unique niches that others can not, making it decently usable across various tiers. It's also better than Necrozma-Dawn Wing thanks to Shadow Shield.
In Gen 7 Ubers and Gen 7 Anything Goes, Lunala is ranked A- and B, respectively in the viability rankings. This is because Ghost is a very good offensive option, and access to Calm Mind and Menacing Moonraze Malestrom via Lunalium Z make it one of the most threatening wallbreakers, being able to one shot many neutral threats. Lunala also has a lot of defensive utility, being able to handle many threats like Necrozma-DM, Ultra Necrozma, Mega Mewtwo Y, Zygarde, etc. thanks to its ability, Shadow Shield.
Meanwhile, Solgaleo is ranked D in Gen 7 Ubers and unranked in Gen 7 AG. It has a very small niche in being a Choice Scarf user that is not affected by Sticky Web, but outside of that it finds itself outclassed. Steel isn't great offensively, and Solgaleo does not have access to Swords Dance unlike Necrozma-DM. Defensively, Full Metal Body does not contribute much as it is not taking super effective hits like Hidden Power [Fire] from Xerneas, Heat Wave from Special Ultra Necrozma, Earth Power from Arceus formes, etc. as well as Necrozma-DM can, thanks to the latter's Prism Armor. Necrozma-Dusk Mane being one of the top ranked Pokemon in the tiers does not help Solgaleo either, as it's facing stiff competition for the Steel-type team slot.
+2 252+ SpA Xerneas Hidden Power Fire vs. 252 HP / 252+ SpD Solgaleo: 226-266 (47.2 - 55.6%) -- 77.3% chance to 2HKO
+2 252+ SpA Xerneas Hidden Power Fire vs. 252 HP / 252+ SpD Prism Armor Necrozma-Dusk-Mane: 147-174 (36.9 - 43.7%) -- 99.6% chance to 3HKO after Leftovers recovery
In Gen 8 Ubers and Gen 8 AG, Lunala is ranked B in both. While a fall in viability may be expected due to Calyrex-Shadow's new dominance as the offensive Ghost-type, Lunala still retains its defensive niche of checking multiple threatening physical attackers. Morever, with the introduction of Heavy-Duty Boots, Lunala does not have to worry much about hazards while switching in, which makes it even better defensively. Solgaleo on the other hand has a slight niche of being a Xerneas/Zacian-C check that is not trapped by Magnezone or Gothitelle via Teleport, but it still can not setup Stealth Rock like Necrozma-DM nor use Dragon Dance sets, which makes it not a very good contender for the Steel-type. Morever, it does not like facing Crunch Zacian-C again, due to not having Prism Armor unlike its competitior. Thus, not much has changed for it and it still languishes in the depth of D rank in the above tiers.
In Gen 9 Ubers UU, Solgaleo is a decent specially defensive wall that is ranked at B-. That's pretty good right? Well, the catch is that Necrozma-Dusk Mane is not allowed in Ubers UU due to having high enough usage in Ubers. So the reason it's any good and is in Ubers UU is simply because of lack of better options, and it not having any niche whatsoever to be worth using in Gen 9 Ubers.
In Gen 9 Ubers, Lunala benefited a ton from Calyrex-Shadow getting banned which freed up its offensive sets very much. Not only that, after the recent Miraidon's ban, Ting-Lu has seen a massive decrease in usage which is a good news for Lunala, as Ting-Lu is capable of shrugging off Lunala's STABs easily and then threatening to phaze it out via Whirlwind, or chip it enough for revenge killers like Koraidon. All of these make Lunala a very scary offensive threat, due to which it is ranked A- in the viability rankings (at the time of writing this). Terastilization also helps Lunala as it can Tera Fairy against Dark-types, 2HKOing or OHKOing them with STAB boosted Moonblast. Alternatively, Tera Ghost Moongeist Beam provides it a convenient way to 2HKO Ho-Oh at +1 (from Meteor Beam or Calm Mind).
tl;dr almost everywhere Solgaleo tries to be good, Necrozma-DM ends up outclassing it. Lunala has more useful tools that make it a viable choice across different generations of AG and Ubers.