You'd probably need to stop all the inbreeding from the start, but if, for some reason, that kinda stuff floats your boat, then there are a couple of solutions. Your OP seems to assume that Carlos II caused the end of the dynasty, and that isn't really true. If he had died young or never been born, then the Spanish Habsburgs would have remained until 1683 (if you accept Infanta Maria Teresa's renunciation as null) or 1673 (if you don't). Any diligent student of history will tell you that both 1683 and 1673 are technically earlier than 1700.
So if we want Spain to continue in the Habsburg sphere of influence, then having Joseph Ferdinand of Bavaria survive is a decent solution, and it only works if Carlos II lives. However, if Carlos II doesn't exist, then the two claimants are the Queen of France and the Holy Roman Empress. Margarita Teresa has the legal right, but it isn't obvious that she's not going to have a son, so the French will do anything to prevent another HRE-Spain personal union. Hence, War of Spanish Succession 1665-???? and the Habsburgs probably lose. However, if they win and Margarita Teresa has a son or more, then OP is satisfied (although there is a new branch of the Spanish Habsburgs).
If Carlos II lives AND Joseph Ferdinand lives, Spain avoids the Bourbons but the Spanish Habsburgs are still extinct, so I don't know whether having a HRE vassal on the throne counts for OP.
If you want the male-line descendants of Charles V on the throne, then one of: John of Austria, Archduke Charles (1607-1632) or Cardinal-Infante Ferdinand (1609-1641) have male issue, who then marry their legitimate cousins and gain a right to the throne in the event of a choice like OTL 1700 - a kind of back-up Bavarian. I suppose John of Austria the younger would be the best choice if Carlos II died young - legitimise him and you avoid war AND the extinction of the family.
Obviously, the best solution is to have Carlos II father a child. Or 'father' a child

The only drawback is that you never need a warming pan in Madrid
