I think Spain government has taken this drastic measure because it has seen a huge spike in gambling from young people of age 18-25 which is the future of the country being impacted by those ads and not being that effective in their society and work.Most teams in Spain are sponsored and has always been sponsored by such ventures,gambling ones and they will lose a good portion of their revenue.Spain has taken this measure because it is affecting its economy by taking a huge portion of the youth absorbed in gambling as I see no other explanation to be such aggressive in a new law.
Unfortunately this is not a long term solution.
The root cause of young people gambling is definitely not this. People don't feel the urge to gamble just because they see a celebrity/athlete wear the banner of a certain sportsbook company on their jersey.
Rather, there is clearly some underlying greater economic issue that needs addressing. Why are people finding the time/motivation to want to gamble? Addressing the symptoms is not a good way to tackle a problem.