This happens because of the salary cap and because of the fact that, in basketball, one player can make a bigger difference for a team than in hockey. The salary cap for NHL teams going into the 2023-2024 season is $84.5 million. The NBA's salary cap for next season is $134 million. That's quite the difference.
Many professional athletes, however, take part in endorsements as an additional source of income. Endorsement deals are common among NHL players, especially Canadian players, due to the exceptionally higher tax margin they are part of. In fact, some of the biggest names in the NHL all have endorsement deals.
Marner, Matthews, and the other names on the list above are all in good hockey markets that allow them to make such endorsement deals. It can be assumed they're adding quite a bit more money to their yearly salary from these opportunities alone. If only they would take a discount on their next contracts because of it.