Warning: SPOILERS ahead for Captain Marvel
Captain Marvel offers some fascinating backstory to the Marvel Cinematic Universe - and may actually explain why Thanos sent Loki to invade Earth in The Avengers. Set in 1995, the movie reveals that Earth was invaded by aliens long before the Chitauri came through a wormhole to attack New York - though the Kree and the Skrulls were a little more subtle with their arrival on planet C-53.
One of the big reveals of Captain Marvel is that the MacGuffin at the center of the movie - which powers an engine that can travel faster than the speed of light - is actually a familiar MacGuffin: the Tesseract, a.k.a. the Space Stone. This is one of the six Infinity Stones that ended up in the hands of Thanos in last year's Avengers: Infinity War, and was the reason that Thanos sent Loki to Earth in The Avengers. The Space Stone has a long history on Earth, having also played a pivotal role in Captain America: The First Avenger, but it may well have been Captain Marvel's discovery of it that tipped Thanos off to its presence on C-53.
Just as the Mind Stone gave Scarlet Witch and Quicksilver their powers, the Space Stone is actually what gave Captain Marvel her powers, in the same explosion that wiped her memories of her life on Earth. Based on what we learned in Captain Marvel, we have a theory that this fateful explosion set in motion the events that eventually led to Loki's attempted takeover of Earth - and that retrieving the Space Stone may not have been Thanos' only goal.
Loki’s Invasion In The Avengers No Longer Makes Total Sense
A lot of pre-planning went into the MCU - particularly from Phase 2 onwards - but there has been some retconning of the early movies that has created potential plot holes. For starters, Thor was originally said the be SHIELD's first contact with alien life, but Captain Marvel has now "revealed" that both Nick Fury and Phil Coulson encountered aliens long before Thor came along. Similarly, Loki's invasion of Earth in The Avengers doesn't quite make sense now that Thanos' long game has been revealed.
Thanos sent Loki to retrieve the Space Stone from Earth, but gave him the Mind Stone in order to help him achieve this goal, and in the process ended up not only getting the Infinity Stone he was after but also losing the one that he already had. It was a convoluted and, quite frankly, bad plan for a supposed master strategist and manipulator. To add even more confusion, Loki's official character breakdown on Marvel's website claims that Loki's outright villainy in The Avengers was due in part to being "influenced" by the Scepter - which eases Loki's eventual redemption arc, but also makes Thanos' plan seem even more complicated and unlikely to work.
Captain Marvel Positioned Herself As Earth’s Protector
Before the Avengers came along to offer their united protection of Earth in 2012, Captain Marvel made a powerful statement to Ronan the Accuser and, by extension, the entire Kree Empire. At the end of Captain Marvel, after destroying bombs that were headed for Earth, Carol Danvers flexed her fully unleashed powers in order to send Ronan and the other Accusers running - in the process sending a powerful message to the entire galaxy that Earth was under her protection. This may well explain why no other alien race tried to invade Earth before the Chitauri.
We know from Ronan and Thanos' alliance in Guardians of the Galaxy that the Mad Titan has links to the Kree Empire, so it seems inevitable that he would have heard about not only Captain Marvel, but also the presence of the Space Stone on Earth. This would not only explain how Earth became a target, but would also explain why Thanos sent in Loki rather than attacking himself.
Was Loki Sent to Test For Captain Marvel's Presence?
Despite his eventual proclamation of, "Fine, I'll do it myself," Thanos generally prefers to operate through lieutenants and pawns - like Loki, Ronan, and the Black Order. Prior to his all-out assault on Earth in Avengers: Infinity War, he first tested the planet's defenses by sending Loki and the Chitauri in to see if C-53 had anything or anyone capable of fighting off an entire alien army. As it turned out, Earth had a team of six superheroes who were ready (after a couple of false starts) to protect the planet. But while they may have emerged victorious, they also gave Thanos a measure of their powers - and their weaknesses as well.
Thanos didn't know about the Avengers before he sent Loki to Earth (they didn't really exist before then, after all), and the events of Captain Marvel suggest that Thanos' real intention was to see whether the woman powerful enough to scare off the Kree Empire was still present on Earth 17 years later. Some fans have questioned why Nick Fury didn't use his pager to call in Captain Marvel when the Chitauri invaded New York, but his failure to do so had bigger consequences than simply making the Avengers' job a little harder. It also sent a message to Thanos that Earth was no longer under Captain Marvel's immediate protection.
If that's the case, then Thanos' seemingly terrible plan of sending in the considerably unstable Loki with one of the Infinity Stones to try and retrieve another Infinity Stone actually makes a lot more sense - because it gives Thanos a victory regardless of whether or not Loki succeeded. If Loki had managed to bring the Space Stone back to Thanos, then that would give him an obvious win. When Loki failed, however, it still gave Thanos a clear picture of the kind of forces defending Earth, and revealed whether or not Captain Marvel was still on the planet.
As Captain Marvel's mid-credits scene show, Carol isn't able to make it back to Earth until long after the dust has settled (literally). She returns when the Avengers are tinkering with Nick Fury's pager, trying to figure out who it was sending a signal to, days or even weeks after the Decimation. This actually gives the Avengers a serious edge over Thanos; Captain Marvel has been a no-show for two major alien invasions of Earth, so at this point Thanos must surely believe that she's either dead or no longer committed to protecting Earth. With the MCU's most powerful superhero on their side, and Thanos totally oblivious, the Avengers are in a good position to win round two of the Infinity War.