As I understand it, the Kingdom of Bohemia and Slovakia under the Kingdom of Hungary would have been administered separately, not to mention that until the 16th century Hungary itself was independent. Was there any sort of "national consciousness" transcending these boundaries? How did the two ethnic groups see themselves in comparison to each other and the outside world?
There were few important reasons as to why Czechoslovakia was formed instead of Czechia and Slovakia.
The dissolution of Austria-Hungary into the individual states is very complicated and there are not exactly other points in history to compare it to other than other countries that were cut up from the same territory.
So, we have to start at the beginning with someone named Tomáš Garrigue Masaryk.
T.G. Masaryk was born in 1850 in a region between Czechia(Moravia) and Slovakia. Which already gives him predisposition to feel for both countries. For a long time he couldnt read or write, his brother ended up teaching him. From there Masaryk started studying. He switched a lot of schools most of which he didnt finish.
(Here I will talk about his life and his build up to popularity/politics and philosophy, feel free to skip it to another: () point, if you are not interested in him)
Since he was 15 he had to make a living for himself, he was a teacher. He then ended up succesfully studying philosophy, and from there he moved to Vienna where he studied it more. He met a young woman from New York, which he ended up marrying.
He liked to write, he became very active with the Pro-Czech community, he started moving into the popular light and his name became known to the public after a controversy. 2 documents that were supposed to come from the 13-14th century rose up. They were named Královedvorský(QueensCourt) and Zelenohorský(GreenMountain).(Not to create confusion, Královédvorský even though it sounds like KingsCourt, it isnt) People split into two groups, majority believed them to be real, a small minority to be fake. Masaryk joined the side of them being fakes, which got him into the spotlight as a person standing behind his word. It got him a lot of criticism and people calling him a traitor of the nation. These two documents and how authentic they are is still debated. But from all the evidence we have, it seems like Masaryk ended up being actually right.
He moved into the Czech universities to teach there as they started separating into Czech and German parts. And there he found his way into politics. He started putting forward a new political view today known as Realism.
He ended up in a another controversy later on in 1899 which gave him even more popularity. At first he wanted Austria-Hungary to stay and be a unitary state with autonomous zones. But when WW1 was declared, his view slowly started to shift.
(Now, we will talk about the actual creation of Czechoslovakia) Masaryk ended up going across Europe to important political personalities, trying to convince them on why the Czech state would be useful.
After he left Austria-Hungary to get his hands on some documents he couldnt get at home, he was exiled by the country, if he came back he would be arrested. He travelled into Italy, then into Switzerland. Where he had a speech, "We are against all forms of violence and we wont use it... But against violence we only can respond with iron"
Masaryk organized a secret society called Maffia.(Not to be confused with a Mafia) He travelled into France, where he was joined by Edvard Beneš(Czech) and Milan Štefánik(Slovak). Beneš ended up being the organizer of the Czechoslovak resistance in France. Masaryk played his role in convincing the surrounding powers into letting a new state show up in Europe. Štefánik also organized the Czechoslovak resistance in Serbia, Italy, and Russia, as a soldier himself he had useful experience in the military to make the small force, a force to be reckoned with.
Masaryk needed this make up of 3 people. A Czech, Beneš. A Slovak, Štefánik. And himself as a person inbetween, thanks to that he would gain the legitimacy and levarage needed to pull strings. A creation of just a Slovak or a Czech state would make them separate, weak. Since the cultures were so similar and Slovakia after its long years of abuse from Hungary wanted freedom, this was the only chance to get out. Masaryk believed in a single state of these two countries under one united identity. He wanted it to become similar to how Czechia is essentialy Bohemia and Moravia.
The resistance was important to show that the people would fight for a country, even though it didnt exist. After the Russian Revolution the Czechoslovak legions got to have its spotlight against the communists. Taking over the entire trans-siberian railway, winning at a naval battle at Lake Baikal, winning at the battle of Zboriv where a lot of future Czechoslovak politicians and generals ended up showing themselves, and being one of the divisions driving towards the house of the Romanovs in chance of freeing them from captivity.
Masaryk started writing newspapers about this new non-existent state, and the popularity of himself, his cabinet in exile, and the idea of a Czechoslovak state as a strong industrial democratic power in Central Europe, started becoming noticable by higher figures in all great powers.
Woodrow Wilson played his important role here. As USA entered the war, Woodrow Wilson was already considering separating the states of Austria-Hungary, and this is just what Masaryk needed. 18th of October 1918 Masaryk came forward with his Washington Declaration. A declaration of a new united Czechoslovak state.
Riots broke out in territories of Austria-Hungary in Czechia and Slovakia, and after the surrender of Austria-Hungary, the state started its function. It still had to deal with wars in the near future after the war, as Hungary tried to take back the territory.
From there on Czechoslovakia became a economic, cultural, democratic and industrial powerhouse of Central Europe. Masaryk was chosen as president 3 times in succesion, he left after he started getting sick, his closest friend and student Beneš ended up being selected as the next president. Štefánik sadly died in a plane crash on the way back home. An alliance named the little Entente was created between Czechoslovakia, Romania and Yugoslavia to stop any further tries of the expansion of Hungary.
In short, Czechoslovakia was created from these two states because if it wouldnt, the states separately probably wouldnt have enough legitimacy and political pull to get out of control of Austria-Hungary. These territories combined were important strategically, Czechia as a important industrial base and Slovakia as a important agrarian base. Slovakia would be taken over by Hungary if Czechia wasnt involved, and Czechia would fall to Germany if Slovakia wasnt involved, the cooperation was crucial for defense of each other strategically. Since their cultures are so similar and they were connected by personalities that came from both countries, their connection was natural. Masaryk himself wanted the connected state because it connected deeply with him in his personal life, as a person that lived in both spaces.
To the allies it was important as a democratic balance of power in the middle of Europe, strong enough to support itself in its defense.
The same question could be put towards Yugoslavia. "Why was Yugoslavia formed instead of Serbia and Croatia and Slovenia separate?" The reasons are political, personal, situational.
As for National identity. I dont know about Slovaks and their identity before the nation. But Czechs had a strong national identity since the days of Bohemia. The culture would be suppresed for a big chunk of their existence, same as their language. Thanks to a national revival created by intelectuals, they recreated and updated the Czech language and culture a few years before the birth of Masaryk. Thanks to that the nationalism of Czechs would be on the rise. Czechs would have a separate identity, same as Slovaks. It would just be connected during WW1 as two close kinds of people, fighting for the same cause. Their similarities made it easier to consolidate, and their common goals and identities made it natural to connect.
(Edit: Gramatical errors)