This is correct - Toyota's entry into racing was 100% marketing, and today is probably 90% marketing. Like you said, if you want to be taken seriously as an "American" brand, being a part of NASCAR is the best way to do it. The engineering aspect is nice, but there are tons of ways Toyota can tinker with engineering other than NASCAR. It's all about selling cars.
When Toyota first entered the sport, they discovered that something like only 20-30% of race fans surveyed would consider buying a Toyota. Today, that number is something close to 45-55%. It's identical to Chevy and a bit better than Ford. Ratings, attendance etc are important, but not nearly as important as that survey. If that number holds steady and/or rises, Toyota will keep doing what they're doing. If that number starts to dip and trend that way, they will likely have to examine their involvement, investment etc. It's pretty much that simple. All about selling cars to people who might never have considered them. The science is a bonus.