I’m glad you asked! The answer is an extremely qualified “It depends”. You just described two fairly common practices, one of which is (somewhat) socially acceptable, the other is not.
When fans write fiction based in the world they admire or want to be a part of, they’ll often introduce original characters into an established world. If one is writing fan fiction, they acknowledge it is fan fiction based on the work of another author, and do not attempt to profit from said fan fiction then technically it is not plagiarism, even if they include phrases from the original work. The keys to avoiding plagiarism in this instance are profit (or lack thereof) and credit (or lack thereof).
While we’re on the subject of fan fiction, some authors don’t mind it, while others really dislike the practice. So the acceptability factor depends on the author. Most authors discourage fan fiction simply because the time spent writing fan fiction could be used to establish their own stories, characters and worlds, so it’s not a great idea no matter what.
If someone changes the name or point-of-view but otherwise leaves the story alone, they try to earn money (or credit, if they’re writing it for a school project) from this effort and they try to claim it as an original work, then absolutely it’s plagiarism. And considering that they’re working on one of the best known universes in modern fiction, it’s a bit silly besides.
There are two somewhat exceptions to this. For example, right after Star Wars came out Del Rey published a really funky novel called “”.
It’s a pretty amazing adventure with Luke, Leia, a fog shrouded planet and Darth Vader. Written by Alan Dean Foster, it has all the trappings of Star Wars except… it never happened. It’s not fan fiction either. It turns out this was sort of a back-up plan just in case Harrison Ford didn’t sign for future films. As time went on Star Wars actually incorporated certain elements into the plot. So even though Alan Dean Foster was working in a world created and owned by Lucasfilm, he was paid to write this at a time when the universe was just being formed and the expanded universe wasn’t even a gleam in George’s eye. Because it was a sanctioned work it’s not considered plagiarism.
The other exception is based on a work that is in the public domain. A good example of this is the book “”.
This is the story of Charlotte Bronte’s famous “Jane Eyre”, but told from the point-of-view of one of the supporting characters. The story is (more or less) the same, but the point-of-view and narrative is changed. The story was written in 1966. Had it been written today, I suspect it would have been considered fan fiction and no one would have paid for it. As it was, the book has enjoyed a steady publication history.
The story “Wicked” is similar – a retelling of the Wizard of Oz from the point-of-view of the Witch. Since the original has long since been in public domain there was no legal claim to make against the characters or world, and the story was original enough for the public to embrace it.
I personally have a few ethical qualms about books like “Wicked”, but legally they can not be considered plagiarism. The same can not be said of any take on the world of Harry Potter, to it’s really a moot point. Harry is subject to some of the strongest copy writes in the industry – so really, don’t even try it.