In both copyright law and trademark law, an artist has the defense of "fair use."
Under copyright law, for instance, a writer may use the trademarked name Coke to describe the type of drink a character is drinking or use Corvette to describe the type of car a character is driving, and under trademark law, an artist may paint a Coke can on a restaurant table and use a Corvette logo on a Corvette painted as part of a street scene. In neither case does the writer/artist show that the work is asociated with, affiliated with, connected with, approved by, endorsed by, authorized by or sponsored by the copyright or trademark holder.
The key in copyright law is often whether there is a duplication of the original or a "substantial similarity" between the newly created work and the original, and in trademark law the key is often whether there is a "likelihood of confusion" between the use of a trademark and the sponsorship of the work and whether the work is used to compete with the original.
Andy Warhol made a career out of using trademarked items in his paintings. In some cases he was sued for infringement, in others he wasn't. His famous Campbell soup can paintings never resulted in a lawsuit by Campbell's, but instead Campbell's capitalized on Warhol's use of the can. For Campbell's, the popularity of Warhol's paintings resulted in "free advertising."
Mattel has often sued, and often over the use of their trademarked Barbie doll (one suit was over the use of "Barbie" in the lyrics of a song and one suit was over the use of Barbie as the subject of photographs).
In Mattel, Inc v Walking Mountain Productions, Mattel brought a trademark infringement suit against photographer Tom Forsysthe for his use of the Barbie doll in his series of "Food Chain Barbie" photos (depicting Barbie doll heads in boiling pots of water, Barbie dolls lined up in baking pans, etc). Mattel lost their suit, as the Court found Forsythe's use to be a fair use of the trademarked doll (for several reasons, one of which was Forsythe's use of the Barbie doll as parody). Forsythe, by the way, was awarded $1,584,089 in attorney fees and $241,797.09 in costs.
The best bet ALWAYS when considering using a trademarked name or a copyrighted work is to get permission from the trademark or copyright owner, or to not use the copyrighted work or trademark at all. Some trademark and copyright holders will sue over any use, while others won't - but any use without authorization is a risk for an artist. An artist has no way of knowing beforehand who will sue and who won't, and any resulting infringement action is guaranteed to be EXTREMELY costly to defend against.